package com.leetcode.LeetCode精选TOP面试题;

/**
 * @author 覃国强
 * @date 2022-05-27 17:21
 */
public class 题库_0069_x的平方根 {

  static
  class Solution {
    public int mySqrt(int x) {
      if (x < 4) {
        return x == 0 ? 0 : 1;
      }
      int l = 2;
      int r = x / 2;
      int answer = 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        long temp = (long) mid * mid;
        if (temp == x) {
          return mid;
        } else if (temp < x) {
          answer = mid;
          l = mid + 1;
        } else {
          r = mid - 1;
        }
      }
      return answer;
    }
  }

}
